package com.example.customer.dao;

import com.example.customer.entity.Contact;
import com.example.customer.entity.CusInfo;
import com.example.customer.entity.Customer;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface CusInfoDAO {

    @Select("select * from tb_customer ")
    List<Customer> queryDataAll();

    @SelectProvider(type = CusInfoDAOProvider.class, method = "queryCondition")
    List<Customer> queryCondition(Customer customer);

    @SelectProvider(type = CusInfoDAOProvider.class, method = "queryLost")
    List<Customer> queryLost(Customer customer);

    @Update("update tb_customer set status = #{status} where id = #{id}")
    void updStatus(String id,String status);

    @Delete("delete from tb_customer where id = #{id}")
    void delInfo(String id);

    @Select("select * from tb_contact where cus_id = #{cus_id}")
    List<Contact> queryByCID(String cus_id);

    //根据id查询
    @Select("select * from tb_contact where id = #{id}")
    Contact queryById(String id);

    @Select("select * from cus_info where id = #{id} or cus_id = #{cus_id}")
    CusInfo queryCusInfo(CusInfo cusInfo);

    @Insert(" insert into cus_info(cus_id,address,post_code,tel,fax,url,regist_license," +
            " artificial_person,registered_capital,annual_turnover,deposit_bank," +
            " bank_account,tax_regist_no,national_tax_regist_no,notes) " +
            " values( #{cus_id}, #{address}, #{post_code}, #{tel}, #{fax}, #{url}," +
            " #{regist_license}, #{artificial_person}, #{registered_capital}, #{annual_turnover}," +
            " #{deposit_bank}, #{bank_account}, #{tax_regist_no}," +
            " #{national_tax_regist_no}, #{notes} )")
    void saveCusInfo(CusInfo cusInfo);

    @Delete("delete from tb_contact where id = #{id}")
    void delContact(String id);

    @UpdateProvider(type = CusInfoDAOProvider.class, method = "modify")
    void modify(Contact contact);


    @UpdateProvider(type = CusInfoDAOProvider.class, method = "updCustomer")
    void updCustomer(Customer customer);

    @UpdateProvider(type = CusInfoDAOProvider.class, method = "updCusInfo")
    void updCusInfo(CusInfo cusInfo);

    /*@Select("select cus.id as cid,cus.*,info.* from tb_customer cus left join cus_info info" +
            " on info.cus_id = cus.cus_id where info.cus_id = #{cus_id}")*/
    @SelectProvider(type = CusInfoDAOProvider.class, method = "queryInfo")
    List<Map> queryInfo(String cus_id);


    /**
     *  mybatis 内部类实现动态sql
     */
    class CusInfoDAOProvider extends SQL {

        public String queryInfo(String cus_id){
            return new SQL(){
                {
                    SELECT("cus.id as cid,cus.cus_id,cus_name,cus_area");
                    SELECT("cus_manager,cus_rank,cus_satisfaction,cus_credit");
                    SELECT("info.*");
                    FROM("tb_customer cus");
                    LEFT_OUTER_JOIN("cus_info info on info.cus_id = cus.cus_id");
                    if(!StringUtils.isEmpty(cus_id)){
                        WHERE("cus.cus_id = '"+cus_id+"' ");
                    }
                }
            }.toString();
        }


        public String queryCondition(Customer customer){
            return new SQL(){
                {
                    SELECT("*");
                    FROM("tb_customer ");

                    if (!StringUtils.isEmpty(customer.getCus_id())) {
                        WHERE("cus_id = #{cus_id} ");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_name())) {
                        WHERE("cus_name like '%"+customer.getCus_name()+"%' ");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_area())) {
                        if(customer.getCus_area().equals("全部")){

                        }else{
                            WHERE("cus_area like #{cus_area} ");
                        }
                    }
                    if (!StringUtils.isEmpty(customer.getCus_manager())) {
                        WHERE("cus_manager like '%"+customer.getCus_manager()+"%' ");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_rank())) {
                        if(customer.getCus_rank().equals("全部")){

                        }else{
                            WHERE("cus_rank like'%"+customer.getCus_rank()+"%' ");
                        }

                    }
                }
            }.toString();
        }

        public String queryLost(Customer customer){
            return new SQL(){
                {
                    SELECT("*");
                    FROM("tb_customer ");

                    if (!StringUtils.isEmpty(customer.getCus_name())) {
                        WHERE("cus_name like '%"+customer.getCus_name()+"%' ");
                    }

                    if (!StringUtils.isEmpty(customer.getCus_manager())) {
                        WHERE("cus_manager like '%"+customer.getCus_manager()+"%' ");
                    }
                        WHERE("status like '%流失%' ");

                }
            }.toString();
        }

        public String modify(Contact contact){
            return new SQL(){
                {
                    UPDATE("tb_contact");
                    if (!StringUtils.isEmpty(contact.getContact())) {
                        SET("contact= #{contact,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(contact.getGender())) {
                        SET("gender= #{gender,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!ObjectUtils.isEmpty(contact.getPosition())) {
                        SET("position= #{position,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(contact.getOffice_tel())) {
                        SET("office_tel= #{office_tel,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(contact.getContact_tel())) {
                        SET("contact_tel= #{contact_tel,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(contact.getNotes())) {
                        SET("notes= #{notes,javaType=String,jdbcType=VARCHAR}");
                    }

                    WHERE("id = #{id,javaType=String,jdbcType=VARCHAR}" );
                }
            }.toString();
        }

        public String updCustomer(Customer customer){
            return new SQL(){
                {
                    UPDATE("tb_customer");
                    if (!StringUtils.isEmpty(customer.getCus_name())) {
                        SET("cus_name= #{cus_name,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_area())) {
                        SET("cus_area= #{cus_area,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!ObjectUtils.isEmpty(customer.getCus_manager())) {
                        SET("cus_manager= #{cus_manager,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_rank())) {
                        SET("cus_rank= #{cus_rank,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_satisfaction())) {
                        SET("cus_satisfaction= #{cus_satisfaction,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_credit())) {
                        SET("cus_credit= #{cus_credit,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_credit())) {
                        SET("cus_credit= #{cus_credit,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(customer.getCus_credit())) {
                        SET("cus_credit= #{cus_credit,javaType=String,jdbcType=VARCHAR}");
                    }

                    WHERE("cus_id = #{cus_id,javaType=String,jdbcType=VARCHAR}" );
                }
            }.toString();
        }

        public String updCusInfo(CusInfo cusInfo){
            return new SQL(){
                {
                    UPDATE("cus_info");
                    if (!StringUtils.isEmpty(cusInfo.getAddress())) {
                        SET("address= #{address,javaType=String,jdbcType=VARCHAR}");
                     }
                    if (!StringUtils.isEmpty(cusInfo.getPost_code())) {
                        SET("post_code= #{post_code,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getTel())) {
                        SET("tel= #{tel,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getFax())) {
                        SET("fax= #{fax,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getUrl())) {
                        SET("url= #{url,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getRegist_license())) {
                    SET("regist_license= #{regist_license,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getArtificial_person())) {
                        SET("artificial_person= #{artificial_person,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getRegistered_capital())) {
                        SET("registered_capital= #{registered_capital,javaType=BigDecimal,jdbcType=DECIMAL}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getAnnual_turnover())) {
                        SET("annual_turnover= #{annual_turnover,javaType=BigDecimal,jdbcType=DECIMAL}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getDeposit_bank())) {
                        SET("deposit_bank= #{deposit_bank,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getBank_account())) {
                        SET("bank_account= #{bank_account,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getTax_regist_no())) {
                        SET("tax_regist_no= #{tax_regist_no,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(cusInfo.getNational_tax_regist_no())) {
                        SET("national_tax_regist_no= #{national_tax_regist_no,javaType=String,jdbcType=VARCHAR}");
                    }

                    WHERE("cus_id = #{cus_id,javaType=String,jdbcType=VARCHAR}" );
                }
            }.toString();
        }

    }

}
